#include "stdafx.h"

#include "CWinPlatformUtilities.h"


CWinPlatformUtilities onlyInstance;



CWinPlatformUtilities::CWinPlatformUtilities()
{

	NOTIMPL
}

CWinPlatformUtilities * CWinPlatformUtilities::getInstance()
{

	return &onlyInstance;

}

char * CWinPlatformUtilities::WideCharToChar(XUINT32 cstr, const XCHAR * pstr, XUINT32 * pcstr, char * pstrDest)
{

	int nLen;
	int nLena;

	char * pRet = NULL;

	if ( pstrDest )
	{
		pRet = pstrDest;
		nLen = *pcstr - 1;
	}
	else
	{
		nLen = WideCharToMultiByte(CP_UTF8, 0, pstr, cstr, NULL, 0, NULL, NULL);
		if ( pcstr )
		{
			*pcstr = nLen;
			return pRet;
		}

		pRet = new char[nLen + 1];
	}
	if ( pRet )
	{
		nLena = WideCharToMultiByte(CP_UTF8, 0, pstr, cstr, pRet, nLen + 1, NULL, NULL);
		pRet[nLena] = NULL;
		if ( pcstr )
			*pcstr = nLena;
	}
	return pRet;

}

XCHAR * CWinPlatformUtilities::CharToWideChar(XUINT32 * cstr, const char * pstr)
{

	int nLen = MultiByteToWideChar(CP_UTF8, 0, pstr, *cstr, NULL, 0);
	void * wstr = NULL;
	if ( nLen )
	{

		wstr = new char[2 * (nLen + 1) | -((unsigned __int64)(unsigned int)(nLen + 1) >> 31 != 0)];
		if ( wstr )
		{
			MultiByteToWideChar(CP_UTF8, 0, pstr, *cstr, (LPWSTR )wstr, nLen);
			*((XCHAR * )wstr + nLen) = NULL;
			*cstr = nLen;
		}
	}
	return (XCHAR * )wstr;

}

XUINT32 * CWinPlatformUtilities::WideCharTo32Char(XUINT32 cstr, const XCHAR * pstr)
{

	NOTIMPL

}

XCHAR * CWinPlatformUtilities::WideChar32ToWideChar(XUINT32 cstr, const XUINT32 * pStr)
{

	NOTIMPL

}

void CWinPlatformUtilities::XFreeCharArray(char * pStr)
{

	NOTIMPL

}

HRESULT CWinPlatformUtilities::XGetInstalledVersion(XCHAR * * ppszInstalledVersion, XUINT32 * pcbInstalledVersion)
{


	HKEY hKey = NULL;
	HRESULT hr S_OK;


	*ppszInstalledVersion = (XCHAR * )new char[0x40];
	*pcbInstalledVersion = 32;
	if ( *ppszInstalledVersion )
	{
		if ( RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\XAML", 0, 0x20019u, &hKey) )
		{
			hr = E_FAIL;
		}
		else
		{
			if ( RegQueryValueExW(hKey, L"Version", 0, 0, (LPBYTE)*ppszInstalledVersion, (LPDWORD)pcbInstalledVersion) )
			{
				hr = E_FAIL;
			}
		}
	}
	else
	{
		hr = 0x8007000Eu;
	}
	if ( hKey )
		RegCloseKey(hKey);
	return hr;

}

XUINT32 CWinPlatformUtilities::Xswprintf_s(XCHAR * pBuf, XUINT32 cchBuf, const XCHAR * pFormat, ...)
{

	int j=0;

	va_list arg_ptr;

	va_start(arg_ptr, j);

	return this->vXswprintf_s(pBuf, cchBuf, pFormat, arg_ptr);

}

XUINT32 CWinPlatformUtilities::vXswprintf_s(XCHAR * pBuf, XUINT32 cchBuf, const XCHAR * pFormat, char * args)
{

	return vswprintf_s(pBuf, cchBuf, pFormat, args);

}

XBOOL CWinPlatformUtilities::IsUILanguageRTL()
{

	LANGID langId;
	__int16 wchLCIDFontSig[16];

	langId = GetUserDefaultUILanguage();
	if ( langId == 3076 )
		langId = 1028;
	return GetLocaleInfoW(langId, 0x58u, (LPWSTR )wchLCIDFontSig, 16) && wchLCIDFontSig[7] & 0x800;

}

XBOOL CWinPlatformUtilities::Is64bitInstalled()
{

	int iType; 

	FARPROC fnIsWow64Process;

	char szBuf[260]; 

	HKEY hKey = 0;
	int iLen = 260;

	fnIsWow64Process = GetProcAddress(GetModuleHandleW(L"kernel32"), "IsWow64Process");
	if ( !fnIsWow64Process )
		return FALSE;
	int bIsWow64 = FALSE;

	if ( !((int (__stdcall *)(HANDLE, int *))fnIsWow64Process)(GetCurrentProcess(), &bIsWow64) || !bIsWow64 )
		return 0;
	if ( !RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\XAML", 0, 0x20119u, &hKey) )
	{
		if ( !RegQueryValueExW(hKey, L"Version", 0, (LPDWORD)&iType, (LPBYTE)szBuf, (LPDWORD)&iLen) )
		{
			RegCloseKey(hKey);
			return TRUE;
		}
		RegCloseKey(hKey);
	}
	return FALSE;

}
